package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.JwtToken;
import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.dto.categoryDto.CategoryAddDto;
import com.itheima.pojo.dto.categoryDto.CategoryUpdateDto;
import com.itheima.pojo.vo.categoryVo.GetCategoryByPageVo;
import com.itheima.pojo.vo.categoryVo.GetCategoryVo;
import com.itheima.service.CategoryService;
import com.itheima.common.AuthThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service

public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper mapper;


    @Override
    public List<GetCategoryByPageVo> selectByPage(String name, Integer page, Integer pageSize) {
               page=page-1;
        return mapper.selectByPage(name,page, pageSize);
    }

    @Override
    public boolean add(CategoryAddDto category) {
        String id = IdUtil.getSnowflakeNextIdStr();
        category.setId(id);
        category.setCreateTime(LocalDateTime.now());
        String auth = AuthThreadLocal.getAuth();
        String parse = JwtToken.parse(auth);
        category.setCreateUser(parse);
        return mapper.add(category)>0;
    }


    @Override
    public boolean delete(String id) {
        return mapper.delete(id)>0;
    }

    @Override
    public boolean update(CategoryUpdateDto category) {
        String auth = AuthThreadLocal.getAuth();
        String parse = JwtToken.parse(auth);
        category.setUpdateUser(parse);
        category.setUpdateTime(LocalDateTime.now());
        return mapper.update(category)>0;
    }

    @Override
    public List<GetCategoryVo> selectAll() {

        return mapper.selectAll();
    }

    @Override
    public String findName(String id) {
        return mapper.findName(id);
    }
}
